package com.tyk.bookstore.back.task.controller;


import cn.dev33.satoken.annotation.SaCheckPermission;
import com.tyk.bookstore.back.common.core.model.result.R;
import com.tyk.bookstore.back.task.model.query.TaskAddQuery;
import com.tyk.bookstore.back.task.model.query.TaskPageQuery;
import com.tyk.bookstore.back.task.model.query.TaskUpdateQuery;
import com.tyk.bookstore.back.task.model.query.TaskUpdateStateQuery;
import com.tyk.bookstore.back.task.model.vo.TaskPageVo;
import com.tyk.bookstore.back.task.model.vo.TaskVo;
import com.tyk.bookstore.back.task.service.TaskService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Set;

/**
 * @author tyk
 * @description 定时任务
 */
@Tag(name = "定时任务管理", description = "定时任务管理")
@RequiredArgsConstructor
@RestController
@RequestMapping("api/task")
public class TaskController {

    private final TaskService taskService;

    @Operation(summary = "查询定时任务详情", description = "查询定时任务详情")
    @SaCheckPermission("platform:task:get")
    @GetMapping("{id}")
    public R<TaskVo> getById(
            @Schema(description = "定时任务ID")
            @NotNull @Min(value = 1)
            @PathVariable Long id
    ) {
        return R.ok(taskService.getById(id));
    }

    @Operation(summary = "分页查询定时任务", description = "分页查询定时任务")
    @SaCheckPermission("platform:task:page")
    @GetMapping("page")
    public R<TaskPageVo> getPage(
            @Schema(description = "定时任务分页查询参数")
            @ParameterObject
            @Validated TaskPageQuery query
    ) {
        return R.ok(taskService.getPage(query));
    }

    @Operation(summary = "添加定时任务", description = "添加定时任务")
    @SaCheckPermission("platform:task:add")
    @PostMapping
    public R<Object> add(
            @Validated @RequestBody TaskAddQuery query
    ) {
        taskService.add(query);
        return R.ok();
    }

    @Operation(summary = "更新定时任务", description = "更新定时任务")
    @SaCheckPermission("platform:task:update")
    @PutMapping
    public R<Object> updateById(
            @Validated @RequestBody TaskUpdateQuery query
    ) {
        taskService.updateById(query);
        return R.ok();
    }

    @Operation(summary = "删除定时任务", description = "删除定时任务")
    @SaCheckPermission("platform:task:delete")
    @DeleteMapping("{id}")
    public R<Object> deleteById(
            @Schema(description = "定时任务ID")
            @Min(value = 1) @NotNull
            @PathVariable Long id
    ) {
        taskService.deleteById(id);
        return R.ok();
    }

    @Operation(summary = "批量删除定时任务", description = "批量删除定时任务")
    @SaCheckPermission("platform:task:delete")
    @DeleteMapping
    public R<Object> deleteBatchByIdSet(
            @Schema(description = "定时任务列表")
            @NotEmpty @NotNull Set<Long> idList
    ) {
        taskService.deleteBatchByIdSet(idList);
        return R.ok();
    }

    @Operation(summary = "修改定时任务状态", description = "修改定时任务状态")
    @SaCheckPermission("platform:task:state:update")
    @PutMapping("state")
    public R<Object> updateState(
            @Validated
            @RequestBody TaskUpdateStateQuery query
    ) {
        taskService.updateState(query);
        return R.ok();
    }

}

